<template>
	<el-container>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'grade.add'">新增等级</el-button>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
					<!-- <el-table-column type="selection" width="50"></el-table-column> -->
					<el-table-column type="index" width="50" align="center"></el-table-column>
					<el-table-column label="等级名称" prop="grade_name" align="center"></el-table-column>
					<el-table-column label="等级条件" prop="grade_name" align="center">
                        <template #default="scope">
                            <div class="grade">
                                <div>月下单数量≥<text style="color:#67c23a">{{scope.row.order_quantity}}</text></div>
                                <el-tag v-if="scope.row.logical == 1" type="Primary">或</el-tag>
							    <el-tag v-if="scope.row.logical == 2" type="danger">且</el-tag>
                                <div>月下单总额≥<text style="color:#67c23a">{{scope.row.order_amount}}</text></div>
                            </div>
						</template>
                    </el-table-column>
					<el-table-column label="操作" align="center">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'grade.show'">查看</el-button>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'grade.edit'">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="danger" size="small" v-auth="'grade.delete'">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>

    <add-grade v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></add-grade>
</template>

<script>
import AddGrade from './addGrade';

export default {
	name: "user",
	components: {
		AddGrade,
	},
	data() {
		return {
			dialog: {
				save: false,
			},
			apiObj: this.$API.customer.getGrade.lists,
			selection: [],
			search: {
				keyword: null,
			},
		};
	},
	watch: {},
	mounted() { },
	methods: {
		//添加
		add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add");
			});
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row);
			});
		},
		//查看
		table_show(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("show").setData(row);
			});
		},
		//删除
		async table_del(row, index) {
			var reqData = { id: row.id };
			var res = await this.$API.customer.getGrade.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
				this.$message.success("删除成功");
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == "add") {
				this.$refs.table.refresh();
			} else if (mode == "edit") {
				this.$refs.table.refresh();
			}
		},
	},
};
</script>

<style>
.grade{
    display: flex;
    align-items: center;
    justify-content: center;
}
</style>
